import mediaLibrary from '@ohos.multimedia.mediaLibrary';

import fileIO from '@ohos.fileio';
import MediaLibOperator from './MediaLibOperator';
import { MediaAsset, MediaOperationAsset, MediaSourceType } from './MediaConstants';
import data_dataShare from '@ohos.data.dataShare';
/*
 *音视频播放服务类
 */
export default class MediaAssetBuilder {

    private mediaLibOperator: MediaLibOperator
    private mediaLibAsset: MediaOperationAsset
    private mediaAsset: MediaAsset
    private realUrl
    private sourceType: MediaSourceType = MediaSourceType.DEFAULT
    private fileDescription: number

    constructor() {
        this.mediaLibOperator = new MediaLibOperator()
    }

    public async build(asset: MediaAsset) {
        this.mediaAsset = asset
        let originSource = this.mediaAsset.getSource()
        console.log('hans ----->><<<<>'+originSource)

        if (!isNaN(originSource)) {
            //Src为数字=fileId
            this.mediaLibAsset = await this.mediaLibOperator.openMediaFileOperationById(originSource)
            this.fileDescription = this.mediaLibAsset.getFd()
            this.sourceType = MediaSourceType.MEDIA_LIB_FILE
            this.mediaAsset.setTitle(this.mediaLibAsset.getAsset().displayName)
            this.realUrl = "fd://" + this.mediaLibAsset.getFd()
        } else if (originSource.startsWith('data/') || originSource.startsWith('/data')) {
            let fd = fileIO.openSync(originSource)
            this.fileDescription = fd
            this.sourceType = MediaSourceType.ABSOLUTE_PATH_FILE
            this.realUrl = "fd://" + fd
        } else if (originSource.indexOf('../resources/rawfile/') >= 0) {
            let rawfileFd = await globalThis.context.resourceManager.getRawFileDescriptor(originSource)
            this.fileDescription = rawfileFd.fd
            this.sourceType = MediaSourceType.RAWFILE_FILE
            this.realUrl = "fd://" + rawfileFd.fd
        } else if (originSource.indexOf('dataability:') >= 0) {
            let list:any = await this.getAssetList(globalThis.appS.Get('isAudio') == true ?mediaLibrary.MediaType.AUDIO:mediaLibrary.MediaType.VIDEO)
            console.log('hans ----->><<<<>222')
            for (var index = 0; index < list.length; index++) {
                const element = list[index];
                if (element.uri == originSource) {
                    this.mediaLibAsset = await this.mediaLibOperator.openMediaFileOperationById(element.id)
                    this.fileDescription = this.mediaLibAsset.getFd()
                    this.sourceType = MediaSourceType.MEDIA_LIB_FILE
                    this.mediaAsset.setTitle(this.mediaLibAsset.getAsset().displayName)
                    this.realUrl = "fd://" + this.mediaLibAsset.getFd()
                    break;
                }
            }
        } else {
            this.realUrl = originSource
        }
        return this.mediaAsset
    }

    public getRealUrl() {
        return this.realUrl
    }
    //获取公共资源
    async getAssetList(MediaType){
        let imageType = MediaType;
        let fileKeyObj = await mediaLibrary.FileKey //文件关键信息
        let getImageOp = {
            selections: fileKeyObj.MEDIA_TYPE + '=?',
            selectionArgs: [imageType.toString()],
            order: fileKeyObj.DATE_ADDED + " DESC",
            extendArgs: "",
            isEditable:true
        };
        const fetchFileResult = await mediaLibrary.getMediaLibrary(globalThis.context).getFileAssets(getImageOp);

        var assetList = []
        await fetchFileResult.getAllObject().then((data)=>{
            for(var i = 0;i<data.length;i++){
                const element = data[i];
            }
            assetList = data
        });
        return assetList
    }

    public async release() {
        switch (this.sourceType) {
            case MediaSourceType.MEDIA_LIB_FILE:
                if (this.mediaLibAsset != null) {
                    await this.mediaLibOperator.closeOMediaFileOperation(this.mediaLibAsset.getAsset(), this.fileDescription)
                    this.mediaLibAsset = null
                }
                break
            case MediaSourceType.ABSOLUTE_PATH_FILE:
                fileIO.closeSync(this.fileDescription)
                break
            case MediaSourceType.RAWFILE_FILE:
                globalThis.context.resourceManager.closeRawFileDescriptor(this.mediaAsset.getSource())
                break
        }
        this.mediaAsset = null
        this.sourceType = MediaSourceType.DEFAULT
        this.fileDescription = -1
    }
}

